<template>
	<view>
		<view class="u-text-center u-margin-top-80 u-font-30">实付金额</view>
		<view class="amount">
			<text class="u-font-38">¥</text>
			<text> {{total | thousands}}</text>
		</view>
		<!-- 	<view class="payment-time">
			<text>支付剩余时间</text>
			<text class="time">14:59</text>
		</view> -->
		<view class="payment-type">
			<view class="payment-type_title">选择支付方式</view>
			<view class="payment-type_item">
				<view class="left">
					<image src="@/static/img/icon_huiyuankoukuan@2x.png"></image>
					<view>
						<view class="payment-name">会员折扣</view>
						<view class="payment-balance">可用余额￥{{member.currentAmount | thousands}}</view>
					</view>
				</view>
				<image src="@/static/img/icon_odd_numbers.png" v-if="paymentType === 0"></image>
				<image src="@/static/img/icon_select.png" v-else @click="handlePaymentType(0)"></image>
			</view>


			<view class="payment-type_item u-margin-top-64">
				<view class="left">
					<image src="@/static/img/icon_WeChat payment@2x.png"></image>
					<view>
						<view class="payment-name">微信支付</view>
					</view>
				</view>
				<image src="@/static/img/icon_odd_numbers.png" v-if="paymentType === 1"></image>
				<image src="@/static/img/icon_select.png" v-else @click="handlePaymentType(1)"></image>

			</view>
		</view>
		<view class="u-margin-left-60 u-margin-right-60">
			<u-button type="success" shape="circle" :custom-style="{
				'background':' #0FBD7F',
				'color':'#fff'
			}" @click="handlePayment">去支付</u-button>
		</view>
		<c-payment-code v-model="shows.code" @submit="handleBalancePayment" />
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		addition
	} from '@/utils/compute.js'
	import cPaymentCode from '@/components/c-payment-code/index.vue'
	export default {
		components: {
			cPaymentCode
		},
		data() {
			return {
				total: '0',
				member: {},
				paymentType: 0,
				ids: [],
				shows: {
					code: false
				},
				type: 0, // 0 订单费用  1 车费用
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type*1
			}
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('BillChecked', (list) => {
				this.total = list.reduce((a, x) => {
					return addition(a, x.amount)
				}, 0)
				this.ids = list.map(x => {
					return x.id
				})
			})
			this.getUserMember()
		},
		methods: {
			async handleBalancePayment(num) {
				uni.showLoading({
					title: '加载中...'
				});
				try{
					await this.$api.getCheckPay(num)
					if (this.type === 0) {
						// 0 订单费用 
						await this.$api.setPaymentOrder(this.ids)
					} else if (this.type === 1) {
						// 1 车费用
						await this.$api.setPaymentCar(this.ids)
					}
					uni.hideLoading();
					this.$refs.uToast.show({
						title: '支付成功',
						callback:()=>{
							uni.navigateBack({
								delta: 1
							})
						}
					})
					
				}catch(e){
					uni.hideLoading();
				}
			},
			handlePayment() {
				const user = uni.getStorageSync('user')
				if (!user.payPassword) {
					this.$toast("未设置支付密码,请前往个人中心设置！");
					return
				}
				if (this.paymentType === 0) {
					this.shows.code = true
				} else if (this.paymentType === 1) {
					this.$toast("微信支付系统维护中");
				}
			},
			handlePaymentType(type) {
				this.paymentType = type
			},
			async getUserMember() {
				const res = await this.$api.getSellerAmount()
				this.member = res.result
			}

		}
	}
</script>

<style scoped lang="less">
	.amount {
		font-size: 32px;
		font-weight: bold;
		color: #202020;
		letter-spacing: 1px;
		margin-top: 15px;
		margin-bottom: 10px;
		text-align: center;
	}

	.payment-time {
		font-size: 13px;
		color: #666666;
		text-align: center;

		.time {
			margin-left: 5px;
			color: #F86920;
		}
	}

	.payment-type {
		margin: 30px 25px 66px 25px;

		.payment-type_title {
			font-size: 14px;
			color: #666666;
			margin-bottom: 27px;
		}

		.payment-type_item {
			display: flex;
			justify-content: space-between;
			color: #202020;

			.left {
				display: flex;

				image:first-child {
					width: 20px;
					height: 20px;
					margin-right: 15px;
				}

				.payment-name {
					color: #202020;
					font-size: 15px;
					margin-bottom: 5px;
				}

				.payment-balance {
					font-size: 12px;
					color: #999999;
				}
			}

			&>image {
				width: 20px;
				height: 20px;
			}
		}
	}
</style>